#!/usr/bin/python

import wx
import wx.stc

FILLER='\nclass MainPanel(wx.Panel):\n\n\tdef __init__(me,parent):\n\t\twx.Panel.__init__(me,parent)\n\t\t\n\t\tme.text=wx.stc.StyledTextCtrl(me)\n\t\t\n\t\tsz=wx.BoxSizer(wx.VERTICAL)\n\t\t\n\t\tme.SetSizer(sz)\n\t\t\n\t\tsz.Add(me.text,1,wx.EXPAND)\n\t\tme.SetAutoLayout(True)\n\t\t\n\t\tme.text.AddText(FILLER)\n\n\t\t\nclass MainFrame(wx.Frame):\n\n\tdef __init__(me):\n\t\t\t\n\t\twx.Frame.__init__(me,parent=None)\n\t\tMainPanel(parent=me)\n\t\t\n\napp = wx.PySimpleApp()\nframe = MainFrame()\nframe.Show()\nframe.Centre()\napp.MainLoop()\n'

import math

class STC:
	STYLE_LINENUMBER=33
	SC_MARGIN_NUMBER=1

settings={}	
#settings['status_fmt']='[%(top_line)s..%(bottom_line)s] Line %(line)s\%(char)s'
settings['status_fmt']='Line %(line)s\%(char)s'

class MainPanel(wx.Panel):

	def __init__(me,parent):
		wx.Panel.__init__(me,parent)
		
		me.parent=parent
		me.text=wx.stc.StyledTextCtrl(me)
		
		sz=wx.BoxSizer(wx.VERTICAL)
		
		me.SetSizer(sz)
		
		sz.Add(me.text,1,wx.EXPAND)
		me.SetAutoLayout(True)
		
		#me.Bind(wx.stc.EVT_STC_MODIFIED, me.OnTextModified)
		me.Bind(wx.stc.EVT_STC_PAINTED,    me.OnTextPainted)
		
		me.text.AddText(FILLER)
		


	def OnTextModified(me,e):
		pass
		
	def OnTextPainted(me,e):
		me.update_status()

	def update_status(me):
		t=me.text
		pos=t.GetCurrentPos()
		try:
			if me.status_last_pos_guard==pos:
				return
		except AttributeError:
			pass
		me.status_last_pos_guard=pos
		top_line = t.GetFirstVisibleLine()+1
		bottom_line = top_line+t.LinesOnScreen()+1
		char=t.GetColumn(pos)
		line=t.LineFromPosition(pos)+1
		status_string=settings['status_fmt']%{'line':line,'char':char,'top_line':top_line,'bottom_line':bottom_line}
		# due to a buglet, at least on osx, delay status bar call until after this event
		# is processed, else you get spurios "CGContextRestoreGState: invalid context" msgs
		#me.parent.SetStatusTextRightField(status_string)
		wx.CallAfter(me.parent.SetStatusTextRightField,status_string)
		print 'setting'
		
		
class MainFrame(wx.Frame):
	
	STATUS_BAR_RIGHT_FIELD=1

	def __init__(me):
			
		wx.Frame.__init__(me,parent=None)
		me.panel=MainPanel(parent=me)
		
		mb=wx.MenuBar()
		me.SetMenuBar(mb)
		m=wx.Menu()
		mb.Append(m,"&Menu")
		id=wx.NewId()
		m.Append(id,'Do Something')
		me.Bind(wx.EVT_MENU,me.OnMenuDoSomething,id=id)
		
		me.toggle=True
		
		sb=wx.StatusBar(me)
		sb.SetFieldsCount(me.STATUS_BAR_RIGHT_FIELD+1)
		sb.SetStatusWidths([-2,-1])
		me.SetStatusBar(sb)
		
		me.SetStatusTextRightField('hello')
		
	def SetStatusTextRightField(me,string):
		me.SetStatusText(string,me.STATUS_BAR_RIGHT_FIELD)
		
	def OnMenuDoSomething(me,e):
		me.toggle=not me.toggle
		
		

app = wx.PySimpleApp()
frame = MainFrame()
frame.Show()
frame.Centre()
app.MainLoop()